core.bash

#!/usr/bin/env bash


##
# Show the help menu
#
function core(){
    run core help
}

##
# Alias for setup()
#
function core_setup_new_project(){
    msg "alias for setup()"
    run core setup
}

##
# Run code scrawl, if it is installed at vendor/taeluf/code-scrawl/
#
function core_scrawl(){
    fPath="$cli_lib_dir/vendor/taeluf/code-scrawl/bin/scrawl"
    if [[ -f  "$fPath" ]];then
        $fPath
    else
        echo "Code Scrawl was not found at '$fPath', so you'll have to run it manually. Check out https://tluf.me/code-scrawl"
    fi
}

##
# Add a script to your ~/.bashrc. Example: ./bash-cli install "$(pwd)/rel_file" 
#
# @arg $1 absolute path to a file to add to your bashrc
# 
function core_install(){
    step "Add '$1' to PATH via ~/.bashrc" bashrc_path_append "$1"
    step_run
}


##
#
# Setup a new project. Each step is optional. Install the bash library globally, add vendor/ to gitignore, write a run script, create the directory structure, write sample scripts, and copy install script & instructions
#
# - Move the cli library to ~/.vendor/bash-cli and symlink to it
# - Add `vendor/` to gitignore file
# - Write the 'run' script for the new project into its `bin` dir and append the project's bin dir to PATH in ~/.bashrc
# - Write the 'install' script for the new project into its root dir
# - Create the directory structure used for a library
# - Output sample group.bash files, internal files, and help files, along with alias functions being present
#
function core_setup(){
    pwd="$(pwd)"
    prompt_yes_or_no "Setup new bash cli project at '$pwd'?" || return

    step "Move cli library to ~/.vendor/bash-cli and symlink?" sys_install_and_symlink 

    step "Add 'vendor/' to .gitignore?" add_vendor_to_git_ignore 

    step "Write 'run' script?" write_run_script

    step "Create directory structure?" create_dir_structure
    step "Write Sample scripts?" write_sample_scripts

    step "Copy install script and instructions?" copy_install_files

    step_run
}


##
# @arg $1 an asbolute path to add to ~/.bashrc
#
function bashrc_path_append(){
    echo "export PATH=\"$1:\$PATH\"" >> ~/.bashrc
}

function sys_install_and_symlink(){
    mkdir -p ~/.vendor
    cur_path="$(realpath "$cli_lib_dir")"
    path="$(realpath ~/.vendor/bash-cli)"
    if [[ "$cur_path" == "$path" ]];then
        msg
        msg_notice "Cli library already in ~/.vendor"   
        msg
        return
    fi

    if [[ -d ~/.vendor/bash-cli/ ]];then
        if prompt_yes_or_no "Delete existing '$path' directory?" ;then
            rm -rf ~/.vendor/bash-cli 
            mv "$cur_path" ~/.vendor/bash-cli
        else
            rm -rf "$cur_path"
        fi
    else
        mv "$cur_path" ~/.vendor/bash-cli
    fi

    ln -s ~/.vendor/bash-cli "$cur_path"
}

function add_vendor_to_git_ignore(){
    echo "vendor/" >> "$pwd/.gitignore"
}

function write_run_script(){
    scriptName=""
    while [[ -z "$scriptName" ]];do
        prompt "Name of your script? ${cInstruct}(q-quit)" scriptName
        if [[ "$scriptName" == "q" ]];then
            msg_status "quit"
        fi
    done

    mkdir -p "$pwd/bin"
    if [[ -f "$pwd/bin/$scriptName" ]];then
        mv "$pwd/bin/$scriptName" "$pwd/bin/$scriptName.$(uniqid).bak"
    fi
    cp "$cli_lib_dir/setup/bin/run" "$pwd/bin/$scriptName"
    msg "$pwd/bin/$scriptName" written

    if prompt_yes_or_no "Add '$pwd/bin' to PATH via ~/.bashrc?";then
        echo "export PATH=\"$pwd/bin:\$PATH\"" >> ~/.bashrc
    fi
}

function create_dir_structure(){
    mkdir -p "$pwd/code/core"
    mkdir -p "$pwd/code/lib"
    mkdir -p "$pwd/code/help"
}

function copy_install_files(){
    cp "$cli_lib_dir/setup/Install.src.md" "$pwd/Install.src.md"
    cp "$cli_lib_dir/setup/install" "$pwd/install"

    msg_instruct "See '$pwd/Install.src.md' for end-user install instructions"

}

function write_sample_scripts(){
    files=(core/core.bash lib/example.bash "help/core.bash")
    for f in "${files[@]}"; do
        path="$pwd/code/$f"
        if [[ ! -f "$path" ]];then
            cp "$cli_lib_dir/setup/$f" "$path"
        fi
    done
}